﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace mb
{
    class MonoLoader : IHotLoader
    {

        const string EntryClassName = "hotfix.Entry";
        const string EntryMethodName = "Start";

        AppDomain appDomain = AppDomain.CurrentDomain;

        public object CreateInstance(string fullName)
        {
            return null;
        }

        public void Load(byte[] data, byte[] symbol)
        {
            /* 
            StringBuilder sb = new StringBuilder();
            var hash = ((HashAlgorithm) CryptoConfig.CreateFromName("MD5")).ComputeHash(data);
            for (int i = 0; i < hash.Length; i++) sb.Append(hash[i].ToString("X2"));
            Console.WriteLine("load hotfix by mono, hash:{0}", sb.ToString());

            var ass=AppDomain.CurrentDomain.GetAssemblies();
            ass.ToList().ForEach(v =>
            {
                if(v.FullName.IndexOf("libmp")>=0)
                    Console.WriteLine("inne as:{0},{1}", v.FullName, v.GetHashCode());
            });
            */

            Assembly dll = Assembly.Load(data, symbol);
            Console.WriteLine("load dll:[{0}], symbol:{1}, hash:{2}", dll.GetName(), symbol.Length, dll.GetHashCode());

            // var desc = dll.GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false).OfType<AssemblyDescriptionAttribute>().FirstOrDefault();
            // var title = dll.GetCustomAttributes(typeof(AssemblyTitleAttribute), false).OfType<AssemblyTitleAttribute>().FirstOrDefault();
            // Console.WriteLine(desc.Description);
            // Console.WriteLine(title.Title);

            var type = dll.GetType(EntryClassName);
            var method = type.GetMethod(EntryMethodName);
            method.Invoke(null, null);
        }

    }

}